<?php

$form = $this->form;
$gallery = $this->gallery;
$thumb_nail = $this->thumbnail;
$form->setAttribute('action', $this->url('admin/child', array('controller'=>'topic','action' => 'edit','id'=>$form->get('id')->getvalue())));
$form->prepare();

?>
<div class="row-fluid sortable ui-sortable">
    <div class="box span12">
        <div class="box-header well">
            <div class="box-icon">
                <a href="<?php echo $this->url('admin/child', array('controller'=>'topic','action'=>'add')) ?>" class="btn btn-setting btn-round"><i class="icon16  icon-add"></i></a>
                <a href="<?php echo $this->url('admin/child', array('controller'=>'topic','action'=>'index')) ?>" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
            </div>
        </div>
        <div class="box-content">
            <legend>Edit Topic</legend>
            <?php
            echo $this->form()->openTag($form);

            echo $this->formHidden($form->get('id'));
            echo '<div class="control-group" >';
            echo '<div class="controls">';
            echo $this->formRow($form->get('create_date')->setValue(date("Y-m-d H:i:s")));
            echo $this->formRow($form->get('user_id'));
            echo '</div>';
            echo '</div>';
            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Title</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('title'));
            echo $this->formRow($form->get('create_date'));

            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Short Description</label>';
            echo '<div class="controls">';

            echo $this->formRow($form->get('short_description'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Full Description</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('full_description'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group">';
            echo '<label class="control-label" for="typeahead">Images</label>';
            echo '<div class="controls" >';
            echo '<img src="'.$this->serverUrl().$thumb_nail.'">';
            echo '</div>';
            echo '</div>';
            echo '<input type="hidden" name="thum_old" value="'.$thumb_nail.'" />';

            echo '<div class="control-group">';
            echo '<label class="control-label" for="typeahead">Cần bán | Mua</label>';
            echo '<div class="controls" >';
            echo $this->formRow($form->get('topic_type'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group">';
            echo '<label class="control-label" for="typeahead">Địa điểm</label>';
            echo '<div class="controls" >';
            echo $this->formRow($form->get('topic_location'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group">';
            echo '<label class="control-label" for="typeahead">Images</label>';
            echo '<div class="controls" >';
            echo $this->formRow($form->get('images'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Price </label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('price'));
            echo '</div>';
            echo '</div>';


            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Show Price </label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('show_price'));
            echo '</div>';
            echo '</div>';



            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Expire Date</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('expire_date'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Status</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('status'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Categories</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('cat_id'));
            echo '</div>';
            echo '</div>';


            //--------------------------------------------------------
            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Is BrandNew?</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('is_brandnew'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Thumb</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('thumb'));
            echo '</div>';
            echo '</div>';

            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Topic Up date</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('up_date'));
            echo '</div>';
            echo '</div>';


            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Slug</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('slug'));
            echo '</div>';
            echo '</div>';


            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Viewed</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('viewed'));
            echo '</div>';
            echo '</div>';


            echo '<div class="control-group" >';
            echo '<label class="control-label" for="typeahead">Is Public?</label>';
            echo '<div class="controls">';
            echo $this->formRow($form->get('is_public'));
            echo '</div>';
            echo '</div>';


            echo '<div class="form-actions" >';
            echo '<div class="">';

            echo $this->formSubmit($form->get('submit'));
            echo '</div>';
            echo '</div>';


            echo $this->form()->closeTag();
            ?>

            <div class="row-fluid">
                <div class="box span12">
                    <div class="box-header well">

                        <h2><i class="icon-th"></i>Add New Gallery</h2>
                    </div>
                    <div class="box-content">
                        <div class="control-group">
                            <label class="control-label" for="selectError">Select Topic For Gellary</label>
                            <div class="controls">
                                <input type="hidden" name="user_id" id="user_id" value="">
                            </div>
                        </div>
                        <div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
                        <br />
                        <div id="container">
                            <span id="pickfiles"  href="javascript:;"><i class="icon icon-plus "></i>[Select files]</span>
                            <span id="uploadfiles" href="javascript:;"><i class="icon-upload"></i>[Upload files]</span>
                        </div>
                    </div>
                </div>
            </div>

            <!--Topic Galery -->
            <div class="row-fluid sortable ui-sortable">
                <div class="box span12">
                    <div class="box-header well" data-original-title="">
                        <h2><i class="icon-picture"></i> Gallery</h2>
                        <div class="box-icon">
                            <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                            <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                            <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
                        </div>
                    </div>
                    <div class="box-content">
                        <p class="center">
                            <button id="toggle-fullscreen" class="btn btn-large btn-primary visible-desktop" data-toggle="button">Toggle Fullscreen</button>
                        </p>
                        <br>
                        <ul class="thumbnails gallery">

                            <?php
                            if($gallery){
                                foreach($gallery as $g) {

                                    ?>
                                    <li id="<?=$g->id?>" class="thumbnail" data-src="<?php echo $g->link ?>">
                                        <a style="background:url(<?php echo $g->link ?>)" href="<?php  echo $g->link ?>" class="cboxElement"><img class="grayscale" src="<?php echo $g->link ?>" >
                                        <input type="hidden" name="thumb" value="<?=$g->link ?>" >
                                        </a>
                                    </li>

                                <?php }
                            }?>

                        </ul>
                    </div>
                </div><!--/span-->

            </div>
            <!--End Topic Galery -->

            <!--Tag -->
            <div class="row-fluid sortable ui-sortable">
                <div class="box span12">
                    <div class="box-header well" data-original-title="">
                        <h2><i class="icon-picture"></i> Tags</h2>
                        <div class="box-icon">
                            <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                            <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                            <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
                        </div>
                    </div>
                </div><!--/span-->
                <div class="box-content">
                    <form class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label" for="typeahead">Tag</label>
                            <div class="controls">
                                <input style="float: left" name="title" type="text" class="input-xlarge focused span6 typeahead" value="" id="txt_tag_name" placeholder="Tag Name">
                                <input style="float: left" class="btn btn-primary span3 typeahead" name="create_date" id="btn_new_tags" type="button" value="Add">
                            </div>
                        </div>
                        <div class="control-group">

                        </div>
                    </form>
                </div>
            </div>
            <!--End Tag -->
            <!--Tag list -->

            <div class="row-fluid sortable ui-sortable">
                <div class="box span12">
                    <div class="box-header well" data-original-title="">
                        <h2><i class="icon-picture"></i> Tags List</h2>
                        <div class="box-icon">
                            <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                            <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                            <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
                        </div>
                    </div>
                </div><!--/span-->
                <div class="box-content">
                    <ul id="list_tag" style="float: left; width: 100%">
                        <?php

                        foreach($this->tag_array as $tag){
                            echo '<li id="topic_tag_item_'.$tag['topic_tag_id'].'" style="float: left; width: 99%; margin-bottom: 10px;">';
                            echo '<input rel="'.$tag['id'].'" type="text" id="topic_tag_item_text'.$tag['topic_tag_id'].'" value="'.$tag['tag_name'].'">';
                            echo '<div style="float:right">';
                            echo '<a href="'.$tag['topic_tag_id'].'" class="btn-edit-tag btn btn-setting btn-round"><i class="icon-cog"></i></a>';
                            echo '<a href="'.$tag['topic_tag_id'].'" class="btn-del-tag btn btn-close btn-round"><i class="icon-remove"></i></a>';
                            echo '</div>';
                            echo '</li>';
                        }
                        ?>
                    </ul>
                </div>
            </div>
            <!--end tag list -->

        </div>
    </div>
</div>


<script type="text/javascript">
    // Custom example logic

    var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : 'pickfiles', // you can pass in id...
        container: document.getElementById('container'), // ... or DOM Element itself
        url : '<?php echo $this->url('admin/child',array('controller'=>'topicimg','action'=>'add')); ?>',
        flash_swf_url : '/js/backend/js/Moxie.swf',
        silverlight_xap_url : '/js/backend/js/Moxie.xap',

        filters : {
            max_file_size : '10mb',
            mime_types: [
                {title : "Image files", extensions : "jpg,gif,png"},
                {title : "Zip files", extensions : "zip"}
            ]
        },

        init: {
            PostInit: function() {
                document.getElementById('filelist').innerHTML = '';
                document.getElementById('uploadfiles').onclick = function() {
                    //add parameter for upload (topic id, user id)
                    uploader.settings.multipart_params = { topic_id: '<?php echo $form->get('id')->getValue(); ?>', user_id: '<?php echo $form->get('user_id')->getValue(); ?>' };
                    uploader.start();
                    return false;
                };
            },

            FilesAdded: function(up, files) {
                plupload.each(files, function(file) {
                    document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
                });
            },

            UploadProgress: function(up, file) {
                document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
            },

            Error: function(up, err) {
                document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
            }
        }
    });

    uploader.init();

    $(document).ready(function(){
     $('#btn_new_tags').click(function(){
        var tag_name = $('#txt_tag_name').val();

         $.ajax({
            url : '<?php echo $this->url('admin/child',array('controller'=>'topic','action'=>'addnewtag')); ?>',
             type: 'POST',
             data: {tag_name: tag_name, topic_id: <?php echo $form->get('id')->getvalue() ?>  }
         }).done(function(val){

                 val = $.parseJSON(val);
                 console.log(val);
                 var _hmtl = '<li id="topic_tag_item_'+val.topic_tag_id+'" style="float: left; width: 99%; margin-bottom: 10px;">';

                 _hmtl+= '<input type="text" rel="'+val.tag_id+'" id="topic_tag_item_text_'+val.topic_tag_id+'" value="'+tag_name+'">';
                 _hmtl+= '<div style="float:right">';
                 _hmtl +='<a href="'+val+'" id="" class="btn-edit-tag btn btn-setting btn-round"><i class="icon-cog"></i></a>';
                 _hmtl +='<a href="'+val+'" id="" class="btn-del-tag btn btn-close btn-round"><i class="icon-remove"></i></a>';
                 _hmtl +='</div>';
                 _hmtl +='</li>'
                 $('#list_tag').append(_hmtl);
         });

     });
        //delete tag by ajax
         $(document).on('click','.btn-del-tag',function(){
             var topic_tag_id = $(this).attr('href');
             $.ajax({
                 url : '<?php echo $this->url('admin/child',array('controller'=>'topic','action'=>'deltag')); ?>',
                 type: 'POST',
                 data: {topic_tag_id: topic_tag_id }
             }).done(function(val){

                $("#topic_tag_item_"+topic_tag_id).remove();
             });
             return false;
         });
        //update tag by ajax;
        $(document).on('click','.btn-edit-tag',function(){
            var topic_tag_id = $(this).attr('href');
            var tag_value  = $("#topic_tag_item_text"+topic_tag_id).val();
            var tag_id = $("#topic_tag_item_text"+topic_tag_id).attr('rel');

            $.ajax({
                url : '<?php echo $this->url('admin/child',array('controller'=>'topic','action'=>'edittag')); ?>',
                type: 'POST',
                data: {tag_name: tag_value,  id: tag_id }
            }).done(function(val){


                });
            return false;
        });


      $.ajax({
            url: "<?php echo $this->url('admin/child',array('controller'=>'tag','action'=>'gettagbytopic')); ?>",
            type: "GET",
            data: { topic_id: <?php echo $form->get('id')->getvalue() ?>  }
      }).done(function(val){
            console.log(val);
      });


    })
</script>